﻿/**
 * Copyright (c) 2013 Nokia Corporation.
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using TweetSharp;
using System.Diagnostics;

namespace Dradis
{
    /// <summary>
    /// The page providing a web browser component for Twitter authentication.
    /// </summary>
    public partial class TwitterLoginPage : PhoneApplicationPage
    {
        // Members
        private TwitterManager _manager = null;

        /// <summary>
        /// Constructor.
        /// </summary>
        public TwitterLoginPage()
        {
            InitializeComponent();
            _manager = TwitterManager.GetInstance();
            _manager.Authenticate(); // Start
            ProgressBarTextBlock.Text = "Loading Twitter login page...";
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="uri"></param>
        private void OnLoadAuthPageUri(object sender, Uri uri)
        {
            Dispatcher.BeginInvoke(() =>
            {
                Browser.Visibility = System.Windows.Visibility.Visible;
                Browser.Navigate(_manager.Service.GetAuthorizationUri(_manager.RequestToken));
            });

        }

        /// <summary>
        /// Called when the authentication attempt returns a result.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="success">True if successful, false otherwise.</param>
        private void OnAuthenticationResult(object sender, bool success)
        {
            Debug.WriteLine("TwitterLoginPage::OnAuthenticationResult(): " + success);
            Dispatcher.BeginInvoke(() => NavigationService.GoBack());
        }

        /// <summary>
        /// Called when the browser finishes loading a page.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnBrowserNavigated(object sender, System.Windows.Navigation.NavigationEventArgs e)
        {
            // Hide the progress bar and the associated text block
            LoadingPageProgressBar.Visibility = Visibility.Collapsed;
            ProgressBarTextBlock.Visibility = Visibility.Collapsed;

            if (_manager.GetAccessTokenFromUri(e.Uri.AbsoluteUri))
            {
                Dispatcher.BeginInvoke(() => {
                    Browser.Visibility = System.Windows.Visibility.Collapsed;
                    LoadingPageProgressBar.Visibility = System.Windows.Visibility.Visible;
                    ProgressBarTextBlock.Text = "Authenticating...";
                    ProgressBarTextBlock.Visibility = Visibility.Visible;
                });
            }
        }

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            _manager.LoadAuthWebPage += new EventHandler<Uri>(OnLoadAuthPageUri);
            _manager.AuthenticationResult += new EventHandler<bool>(OnAuthenticationResult);
        }

        protected override void OnNavigatedFrom(NavigationEventArgs e)
        {
            _manager.LoadAuthWebPage -= OnLoadAuthPageUri;
            _manager.AuthenticationResult -= OnAuthenticationResult;
        }
    }
}